package com.yantu.controller.student;

import com.yantu.bean.ExamPaper;
import com.yantu.bean.Question;
import com.yantu.bean.TextContent;
import com.yantu.mapper.ExamPaperMapper;
import com.yantu.mapper.QuestionMapper;
import com.yantu.mapper.TextContentMapper;
import com.yantu.util.Result;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
@ApiModel("试卷查询")
public class StudentPaperSelectController {


    @Autowired
    ExamPaperMapper examPaperMapper;
    @Autowired
    TextContentMapper textContentMapper;
    @Autowired
    QuestionMapper questionMapper;


    //试卷查询
    @GetMapping("/paperSelect/{id}")
    @ApiOperation("试卷查询")
    public Result paperSelect(@PathVariable("id") Integer id) {
        ExamPaper examPaper = examPaperMapper.selectById(id);
        List<Object> list = new ArrayList();
        //每一张试卷
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",examPaper.getId());
        map.put("level",examPaper.getGradeLevel());
        map.put("subjectId",examPaper.getSubjectId());
        map.put("paperType",examPaper.getPaperType());
        map.put( "name",examPaper.getName());
        map.put("suggestTime",examPaper.getSuggestTime());
        map.put( "limitDateTime",null);


        List<Object> list1 = new ArrayList();
        HashMap<String, Object> map1 = new HashMap<>();
        //将Exam_paper表和text_content对应起来
        TextContent textContent = textContentMapper.selectById(examPaper.getFrameTextContentId());

        //获取content表中的content内容
        //"titleItems"
        //JSONArray(list类型)
        //JSONObject（Map类型）
        JSONArray contentArray = JSONArray.fromObject(textContent.getContent());
        JSONObject content=contentArray.getJSONObject(0);
        map1.put("name",content.getString("name"));
        List<Object> list2 = new ArrayList();


        //获取大标题内的题目列表
        // "questionItems"
        JSONArray questionItemArray=JSONArray.fromObject(content.getString("questionItems"));

        for (int i = 0; i <questionItemArray.size() ; i++) {
            JSONObject qusetion = questionItemArray.getJSONObject(i);
            HashMap<String, Object> map2 = new HashMap<>();
            map2.put("id",qusetion.getString("id"));
            map2.put("itemOrder",qusetion.getString("itemOrder"));
            Question questionItem = questionMapper.selectById(qusetion.getString("id"));
            map2.put("questionType",questionItem.getQuestionType());
            map2.put("subjectId",questionItem.getSubjectId());
            map2.put("score",questionItem.getScore());
            map2.put("gradeLevel",questionItem.getGradeLevel());
            map2.put( "difficult",questionItem.getDifficult());
            map2.put("correct",questionItem.getCorrect());
            String[] answer = questionItem.getCorrect().split(",");
            List<String> correctList = new ArrayList<>();
            for (int j = 0; j < answer.length; j++) {
                correctList.add(answer[j]);
            }
            map2.put("correctArray",correctList);
            if(questionItem.getInfoTextContentId()!=null){


                TextContent textContent1 = textContentMapper.selectById(questionItem.getInfoTextContentId());
//                    JSONArray contentArray1 = JSONArray.fromObject(textContent1.getContent());
////                    JSONObject content1=contentArray1.getJSONObject(0);
                JSONObject content1 = JSONObject.fromObject(textContent1.getContent());

                map2.put("title",content1.getString("titleContent"));
                map2.put("analyze",content1.getString("analyze"));
                JSONArray contentObject1 = JSONArray.fromObject(content1.getString("questionItemObjects"));
                List<Object> answList = new ArrayList<>();
                if(contentObject1.size()==0){
                    map2.put("items",null);
                }else{
                    for (int j = 0; j <contentObject1.size() ; j++) {
                        JSONObject xuanxiang = contentObject1.getJSONObject(i);
                        Map<String,String> map4 = new HashMap();
                        map4.put("prefix", xuanxiang.getString("prefix"));
                        map4.put("content", xuanxiang.getString("content"));
                        map4.put("score", xuanxiang.getString("score"));
                        answList.add(map4);
                    }
                }
                map2.put("items",answList);
            }
            list2.add(map2);
        }
        map1.put("questionItems",list2);
        list1.add(map1);
        map.put("titleItems",list1);
        map.put("score",examPaper.getScore());
        list.add(map);
        return Result.succ(200, "查询成功", list);
    }
}
